package com.suriya.thirukural;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Map.Entry;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class ThirukuralContext extends Application
{
    static final int          kuralCount[][] = { { 4, 20, 13, 1 }, { 25, 10, 2, 1, 2, 17, 13 }, { 7, 18 } };
    private SharedPreferences _pref;

    public Drawable getBackgroundImage()
    {
        Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int color[] = new int[4];
        color[0] = Color.rgb(107, 203, 255);
        color[1] = Color.rgb(239, 251, 255);
        color[2] = Color.rgb(16, 154, 206);
        color[3] = Color.rgb(0, 48, 90);
        GradientDrawable grad = new GradientDrawable(Orientation.TOP_BOTTOM, color);
        grad.setSize(display.getWidth(), display.getHeight());
        grad.setBounds(0, 0, display.getWidth(), display.getHeight());
        grad.setDither(true);

        return grad;
    }

    public void init()
    {
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
        _pref = getSharedPreferences("widgets", Context.MODE_PRIVATE);
        _loadWidgets();
    }

    static class WidgetInfo
    {
        Set<Integer> kuralTopics = new TreeSet<Integer>();
        boolean      background  = false;

        public WidgetInfo()
        {            
        }
        
        public WidgetInfo(String inStr)
        {
            String params[] = inStr.split("\\+");
            
            if(params.length >= 1)
            {
                String selIdsStr[] = params[0].split(",");
                for(int idx_1 = 0; idx_1 < selIdsStr.length; idx_1++)
                {
                    String curr = selIdsStr[idx_1];
                    kuralTopics.add(Integer.parseInt(curr));
                    Log.d("Suriya", "Adding for widget Sec:" + curr);
                }
            }
            if(params.length >=2)
            {
                String bgFlag = params[1];
                background = Boolean.parseBoolean(bgFlag);
            }
        }
        
        public String toString()
        {
            String ret = "";

            for(Integer curr : kuralTopics)
            {
                ret = ret + curr + ",";
            }
            ret += "+";
            ret += "" + background;
            return ret;
        }
    }

    Map<Integer, WidgetInfo> widgets = new TreeMap<Integer, WidgetInfo>();

    public void saveWidget(int mAppWidgetId, WidgetInfo inInfo)
    {
        widgets.put(mAppWidgetId, inInfo);
        _saveWidgets();
    }

    public void removeWidget(int mAppWidgetId)
    {
        widgets.remove(mAppWidgetId);
        _saveWidgets();
    }

    private void _loadWidgets()
    {
        widgets.clear();
        String val = _pref.getString("widgets", "");

        String entries[] = val.split("-");

        for(int idx = 0; idx < entries.length; idx++)
        {
            String entry = entries[idx];
            String rows[] = entry.split(":");

            if(rows.length != 2) continue;

            Integer id = Integer.parseInt(rows[0]);
            WidgetInfo widgetInfo = new WidgetInfo(rows[1]);

            Log.d("Suriya", "Adding WidgetID:" + id);
            widgets.put(id, widgetInfo);           
        }
    }

    private void _saveWidgets()
    {
        StringBuilder builder = new StringBuilder();

        for(Entry<Integer, WidgetInfo> entry : widgets.entrySet())
        {
            builder.append("" + entry.getKey());
            builder.append(":");
            builder.append("" + entry.getValue());
            builder.append("-");
        }

        String val = builder.toString();

        _pref.edit().putString("widgets", val).commit();
    }
}
